<template>
  <div class="base-header" :class="mode">

    <div class="base-header__logo" :class="fixed">
      <div class="base-header__warp flex">
        <AppLogo />
        <div class="telephone">
          <span class="margin-right-20">{{parseTime(time,'{y}-{m}-{d} {h}:{i}:{s}')}}</span>
          <span>
            <a href="http://jsy.xjjs.gov.cn:5002/asite/cloud/index" target="_blank">返回旧版</a>
          </span>
        </div>
      </div>
    </div>

    <div class="base-header__menuNav" :class="fixed">
      <div class="base-header__warp">
        <div class="menu-nav" :class="mode" v-if="menuNav">
          <RouterLink class="menu-nav__item" v-for="menu in menus" :key="menu.path" :to="menu.path">{{menu.title}}</RouterLink>
        </div>
      </div>
    </div>

  </div>
</template>

<script lang="ts">
import {defineComponent, computed, provide, ref, onMounted  , onBeforeUnmount} from 'vue';
import { useRoute } from 'vue-router';

import AppLogo from './AppLogo.vue';
import { parseTime } from "/@/utils";
interface menuItem {
  path: string;
  title: string;
}


export default defineComponent({
  components: { AppLogo },
  setup() {
    const menus = ref<menuItem[]>([
      { path: '/home', title:'首页'},
      { path: '/public', title:'信息公开'},
      { path: '/service', title:'数据服务'},
      { path: '/theme', title:'主题服务'},
      { path: '/help', title:'帮助中心'}
    ])
    const time  = ref<any>();
    const Route = useRoute();
    const menuNav = ref(true);
    const fixed = ref<string>('');
    let mode = computed(():string => {
      let path = Route.path;
      let menuNavArr = ['articleDetail/','themeService/'];
      menuNav.value = !(path.indexOf('articleDetail/' || 'themeService/')>-1);
      menuNavArr.forEach(v => {
        if(path.indexOf(v)>-1) menuNav.value = false
      });
      const pathArr = ['/home', '/articleList', '/service', '/service/', '/theme', '/help'];
      let keyWord = path.split("/");
      let haveKeyWord = keyWord.some(item=>item==='public')
      let haveKeyWord2 = keyWord.some(item=>item==='articleDetail')
      if(haveKeyWord || haveKeyWord2){
        return 'transparent'
      }else {
        return pathArr.indexOf(path)>-1?'transparent':'white';
      }
    })

    provide('mode', mode);

    const init = () => {
      handleScroll();

      setInterval(function (){
        time.value = new Date()
      },1000)
    }

    function handleScroll() {
      window.addEventListener('scroll', () => {
        console.log(window.scrollY);
        if(window.scrollY>0){
          fixed.value = 'fixed'
        }else {
          fixed.value = ''
        }
      });
    }


    // 离开当前组件别忘记移除事件监听
    onBeforeUnmount(() => {
      window.removeEventListener('scroll', () => {});
    });

    onMounted(init)

    return {
      mode,
      menuNav,
      time,
      parseTime,
      fixed,
      menus
    }
  }
})
</script>

<style lang="less" scoped>
.base-header {
  position: fixed;
  top: 0;
  z-index: 999;
  width: 100%;
  color: @white;
  border-bottom: 1px solid fade(@white,15%);

  * {
    transition: all .4s ease-in-out;
  }

  .margin-right-20{
    margin-right: 20px;
  }

  &.white {
    position: fixed;
    color: @black;

    .base-header__logo{
      background: @blue;
    }

    .telephone {
      color: @white;

      i {
        color: @white;
      }
    }

    .base-header__menuNav {
      background: white;
      box-shadow: 0 3px 6px rgba(0, 0, 0, 0.08);
    }
  }

  .base-header__warp {
    display: flex;
    justify-content: space-between;
    width: @main-width;
    height: 100%;
    margin: 0 auto;

    .telephone {
      display: flex;
      align-items: center;
      color: @white;
    }
  }

  .base-header__logo{
    height: 60px;
    background: transparent;

    &.fixed {
      background: @blue;

      .telephone {
        color: @white;

        i {
          color: @white;
        }
      }
    }


  }

  .base-header__menuNav {

    &.fixed {
      background: white;
      box-shadow: 0 3px 6px rgba(0, 0, 0, 0.08);

      .menu-nav{
        .menu-nav__item {
          color: @black;

          &.router-link-active, &:hover {
            color: #0078FF;

            &::after {
              background: #0078FF;
            }
          }
        }
      }
    }

    .menu-nav {
      display: flex;
      background: transparent;

      &.white {
        .menu-nav__item {
          color: @black;

          &.router-link-active, &:hover {
            color: #0078FF;

            &::after {
              background: #0078FF;
            }
          }
        }
      }

      .menu-nav__item {
        position: relative;
        margin-right: 60px;
        font-size: 18px;
        line-height: 56px;
        color: @white;

        &:hover {
          color: @white;
        }

        &.router-link-active::after {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 2px;
          background: @white;
          border-radius: 1px;
          content: "";
        }
      }
    }

  }


}


</style>
